package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.scity.beens.YijianBeen;
import com.example.scity.beens.ZhjgyijianBeen;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ZhjgYjfkActivity extends AppCompatActivity {
    EditText zhjgtitle;
    EditText zhjgcontent;
    Button zhjgbtntijiao;
    Button zhjgbtnlist;
    ZhjgyijianBeen zhjgyijianBeen;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Toast.makeText(ZhjgYjfkActivity.this,ZhjgyijianBeen.msg,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhjg_yjfk);
        zhjgtitle = findViewById(R.id.zhjg_yijian_title);
        zhjgcontent = findViewById(R.id.zhjg_yijian_content);
        zhjgbtntijiao = findViewById(R.id.zhjgyijian_button);
        zhjgbtnlist=findViewById(R.id.zhjg_yijian_fklb);

        //loadzhjgyijian();
        //intoZhjgFklb();
    }
    private void loadzhjgyijian() {
        zhjgbtntijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String,String> map=new HashMap<String,String>();
                map.put("title", zhjgtitle.getText().toString());
                map.put("content", zhjgcontent.getText().toString());
                OkHttpClient okHttpClient = new OkHttpClient();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody requestBody = RequestBody.create(mediaType, new Gson().toJson(map));
                Request request = new Request.Builder()
                        .url(Configs.Host + APIInterface.ZHJGYJFK)
                        .addHeader("Authorization", Configs.Token)
                        .post(requestBody)
                        .build();
                okHttpClient.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {

                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        zhjgyijianBeen = new Gson().fromJson(response.body().string(), ZhjgyijianBeen.class);
                        handler.sendEmptyMessage(0);
                    }
                });
            }
        });
    }

    private void intoZhjgFklb() {
        zhjgbtnlist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ZhjgYjfkActivity.this,ZhjgFklbActivity.class));
            }
        });
    }
}